package com.sm.ui;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.domain.Course;
import com.sm.service.ICourseService;
import com.sm.util.JTableUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

@Component
public class CourseUI implements BaseUI {

    @Autowired
    private ICourseService service;
    private JPanel panel;
    private JScrollPane scrollPane;
    private JTable table;
    private JTabbedPane tablePane;

    @Override
    public void init(JTabbedPane tablePane){
        this.tablePane = tablePane;
        panel = new JPanel();
        createSelect();
        createBtn();
        createList();
    }

    private void createSelect(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel lNo = new JLabel("课程：");
        JTextField tNo = new JTextField(16);
        JButton btnAdd = new JButton("查询");
        jb.add(lNo);
        jb.add(tNo);
        jb.add(btnAdd);
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                DefaultTableModel dtm = (DefaultTableModel) table.getModel();
                Course vo = new Course();
                vo.setCourseName(tNo.getText());
                select(dtm, vo);
            }
        });
        panel.add(jb);
    }

    private void createBtn(){
        JPanel jb = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton btnAdd = new JButton("新增"), btnDelete = new JButton("删除"), btnUpdate = new JButton("修改");
        jb.add(btnAdd);
        jb.add(btnUpdate);
        jb.add(btnDelete);
        btnAdd.addActionListener(e -> createPage("新增", 1));
        btnUpdate.addActionListener(e -> createPage("修改", 2));
        btnDelete.addActionListener(e -> delete());
        panel.add(jb);
    }


    private void createPage(String title, int flag){
        if (flag == 2) {
            if (table.getSelectedRowCount() != 1) {
                JOptionPane.showMessageDialog(null, "请选择一项信息！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        JPanel addP = new JPanel(new GridLayout(2, 1));
        tablePane.addTab(title, addP);
        tablePane.setSelectedIndex(tablePane.getTabCount() - 1);
        // 实例化组件
        JLabel lblName = new JLabel("课程名：");
        JTextField txtId = new JTextField(16);
        txtId.setVisible(false);
        JTextField txtName = new JTextField(16);

        JButton btnOk = new JButton("确定");
        if (flag == 2) {
            int row = table.getSelectedRow();
            Integer id = (Integer) table.getValueAt(row, 0);
            Course bo = service.getById(id);
            txtName.setText(bo.getCourseName());
        }
        // 将组件分组放入面板，然后将小面板放入主面板
        add2P(addP, lblName, txtName, txtId);
        add2P(addP, btnOk);
        // 注册监听器，监听确定按钮
        btnOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                // 获取用户输入的数据
                String userName = txtName.getText().trim();
                // 将数据封装到对象中
                Course bo = new Course();
                bo.setCourseName(userName);
                if (flag == 2) {
                    int row = table.getSelectedRow();
                    Integer id = (Integer) table.getValueAt(row, 0);
                    bo.setId(id);
                }
                // 保存数据
                if (service.saveOrUpdate(bo)) {
                    // 输出提示信息
                    //System.out.println("注册成功！");
                    JOptionPane.showMessageDialog(null, "成功！", "成功提示", JOptionPane.PLAIN_MESSAGE);
                    tablePane.removeAll();
                    init(tablePane);
                }
                else {
                    // 输出提示信息
                    //System.out.println("注册失败！");
                    JOptionPane.showMessageDialog(null, "失败！", "错误提示", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    private void add2P(JPanel addP, java.awt.Component... comp){
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        for (java.awt.Component component : comp) {
            p2.add(component);
        }
        addP.add(p2);
    }

    private void delete(){
        if (table.getSelectedRowCount() != 1) {
            JOptionPane.showMessageDialog(null, "请选择一项信息！", "错误", JOptionPane.ERROR_MESSAGE);
        }
        else {

            int ok = JOptionPane
                    .showConfirmDialog(null, "是否删除该信息？", "确定", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
            if (ok == JOptionPane.YES_OPTION) {
                int row = table.getSelectedRow();
                Integer id = (Integer) table.getValueAt(row, 0);
                service.removeById(id);
                JOptionPane.showMessageDialog(null, "删除成功！");
                tablePane.removeAll();
                init(tablePane);

            }
        }
    }

    private void createList(){
        tablePane.addTab("课程管理", panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        String[] columnNames = {"ID", "课程"};
        DefaultTableModel dataModel = new DefaultTableModel();
        dataModel.setColumnIdentifiers(columnNames);
        select(dataModel, null);
        table = new JTable(dataModel) {
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        JTableUtil.setTableColumnCenter(table);
        JTableUtil.setbgcolor(table);
        // 通过JTable对象创建JScrollPane，显示数据
        scrollPane = new JScrollPane(table);
        panel.add(scrollPane);
    }

    private void select(DefaultTableModel dataModel, Course vo){
        dataModel.setRowCount(0);
        QueryWrapper<Course> qw = getQueryWrapper(vo);
        List<Course> list = service.list(qw);
        for (Course l : list) {
            Vector v = new Vector();
            v.add(l.getId());
            v.add(l.getCourseName());
            dataModel.addRow(v);
        }
    }

    private QueryWrapper<Course> getQueryWrapper(Course vo){
        QueryWrapper<Course> qw = new QueryWrapper();
        if (vo == null) {
            return qw;
        }
        if (StringUtils.isNotEmpty(vo.getCourseName())) {
            qw.eq("courseName", vo.getCourseName());
        }
        return qw;
    }


}

